/*
 * 软件版权: Mr.LEE
 * 修改记录:
 * 修改日期     修改人员  修改说明
 * ========    =======  ============================================
 * 2020/12/23  LJH  新增
 * ========    =======  ============================================
 */
package com.lagou.cloud.controller;

import com.lagou.cloud.pojo.LagouUserDto;
import com.lagou.cloud.service.AuthTokenFeignService;
import com.lagou.cloud.service.EmailFeignService;
import com.lagou.cloud.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 功能说明:
 *
 * @author LJH
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private AuthTokenFeignService authTokenFeignService;
    @Autowired
    private EmailFeignService emailFeignService;

    @GetMapping(value = "/getToken")
    public String getAuthToken(@RequestParam("email") String email) {
        //调用验证码服务获取验证码
        String authToken = authTokenFeignService.getAuthToken(email);
        //调用邮件服务发送邮件
        String result = emailFeignService.sendEmail(email, authToken);
        if ("fail".equals(result)) {
            return "验证码获取失败！";
        }
        return "验证码获取成功，已发送至邮箱！";
    }

    @PostMapping(value = "/register")
    public String register(@RequestBody LagouUserDto lagouUserDto) {
        if (lagouUserDto == null) {
            return "注册失败！";
        }
        boolean register = userService.register(lagouUserDto.getEmail(), lagouUserDto.getPassword(), lagouUserDto.getAuthToken());
        if (register) {
            return "注册成功！";
        } else {
            return "注册失败！";
        }
    }

    @PostMapping(value = "/login")
    public String login(@RequestBody LagouUserDto lagouUserDto) {
        if (lagouUserDto == null) {
            return "登录失败！";
        }
        boolean login = userService.login(lagouUserDto.getEmail(), lagouUserDto.getPassword());
        if (login) {
            return "登录成功！";
        } else {
            return "登录失败！";
        }
    }
}
